from qturboframe.public_ui.base_element import BaseElement
from qturboframe.lib.report.log import logger
from qturboframe.lib.webdriver.exceptions import SeleniumError
from qturboframe.lib.utils.check import CheckBasic
from qturboframe.lib.utils.CustomError.custom_error import CustomError


class TextArea(BaseElement, CheckBasic):
    '''hui多行文本框操作类'''

    def __init__(self, element_para, father=None, timeout=5):
        super().__init__(element_para, father, timeout)
        class_list = self._element.get_attribute('class').split(' ')
        if 'h-input-wrapper' not in class_list:
            self._element = self._element.find_element_by_attribute(
                'xpath', './ancestor::div[contains(@class, "h-input-wrapper")]', timeout=self.timeout)
        self.textarea = self._element.find_element_by_attribute(
            'css selector', '.h-input', timeout=self.timeout)

    def set_value(self, value):
        '''
        hui多行文本框填充

        :Args:
         - text: 填充值

        :Example:
         - 例: textarea.set_value('苹果吃葡萄')
        '''
        self.textarea.focus()
        self.textarea.clear()
        self.textarea.send_keys(value)

    def clear(self):
        '''
        hui多行文本框清空

        :Example:
         - 例: textarea.clear()
        '''
        self.textarea.focus()
        self.textarea.clear()

    def is_default(self, value):
        """
        检查hui多行文本框件默认值

        :Args:
         - value: 待检查的值

        :Returns:
            检查匹配返回True，否则返回False

        :Example:
         - 例: textarea.is_default('苹果吃葡萄')
        """
        self.textarea.focus()
        if self.textarea.text:
            text = self.textarea.text
        elif self.textarea.get_attribute('defaultvalue'):
            text = self.textarea.get_attribute('defaultvalue')
        elif self.textarea.get_attribute('value'):
            text = self.textarea.get_attribute('value')
        elif self.textarea.get_attribute('placeholder'):
            text = self.textarea.get_attribute('placeholder')
        elif self.textarea.get_attribute('title'):
            text = self.textarea.get_attribute('title')
        else:
            text = ''
        if text == value:
            return True
        else:
            logger.error('默认值不匹配！')
            return False

    def is_readonly(self):
        '''
        检查hui多行文本框是否只读

        :Return:
            返回文本框是否只读

        :Example:
         - 例: textarea.is_readonly()
        '''
        self.textarea.focus()
        return self.textarea.is_readonly()

    def is_nonnull(self):
        '''
        检查hui多行文本框是否有非空属性

        :Returns:
            非空返回True，否则返回False

        :Example:
         - 例: textarea.is_nonnull()
        '''
        self.textarea.focus()
        form_item = self._element.find_element_by_attribute(
            'xpath', './ancestor::div[contains(@class, "h-form-item-content")]/'
                     'parent::div[contains(@class, "h-form-item")]', timeout=self.timeout)
        item_class_list = form_item.get_attribute('class').split(' ')
        if 'h-form-item-required' in item_class_list:
            return True
        else:
            return False

    def check_default(self, value):
        """
        校验hui多行文本框件默认值

        :Args:
         - value: 待检查的值

        :Raises:
            SeleniumError: 多行文本框的默认值校验不通过

        :Example:
         - 例: textarea.check_default('苹果吃葡萄')
        """
        if not self.is_default(value):
            raise SeleniumError(CustomError.Error_EQ0301800_TextArea_Verify_Fail,'多行文本框的默认值校验不通过！')

    def check_readonly(self):
        '''
        校验hui多行文本框是否只读

        :Raises:
            SeleniumError: 多行文本框的只读校验不通过

        :Example:
         - 例: textarea.check_readonly()
        '''
        if not self.is_readonly():
            raise SeleniumError(CustomError.Error_EQ0301800_TextArea_Verify_Fail,'多行文本框的只读校验不通过！')

    def check_nonnull(self):
        '''
        校验hui文本框是否有非空属性

        :Raises:
            SeleniumError: 多行文本框的非空校验不通过

        :Example:
         - 例: textarea.check_nonnull()
        '''
        if not self.is_nonnull():
            raise SeleniumError(CustomError.Error_EQ0301800_TextArea_Verify_Fail,'多行文本框的非空校验不通过！')

    def get_value(self):
        v = self.textarea.get_attribute("value")
        if v:
           v = v.replace("\r","").replace("\n","").strip()
        return v

    def check(self, ui_expected):
        actual_text = self.get_value()
        if actual_text == ui_expected.strip():
            logger.info("期望值为：{}".format(ui_expected))
            logger.info("实际值为：{}".format(actual_text))
        else:
            logger.error("期望值为：{}".format(ui_expected))
            logger.error("实际值为：{}".format(actual_text))
            raise SeleniumError(CustomError.Error_EQ0301801_TextArea_Expect_Actual_Not_Match,"期望值和实际值不符")


